//
//  ZCOrderViewModel.m
//  ZCJZ
//
//  Created by sujeking on 2019/2/28.
//  Copyright © 2019年 jeking. All rights reserved.
//

#import "ZCOrderViewModel.h"
#import <CommonCrypto/CommonCrypto.h>
#import "SKBaseWebService.h"
#import "AppAndDeviceTool.h"

@implementation ZCOrderViewModel

+ (void)confirmOrderWithInfo:(NSDictionary *)info
                    complete:(void (^)(ZCErrorCode status, NSString *message,id data))block {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithDictionary:info];
    [SKBaseWebService getRequest:@"/v1/order/confirm" parameters:parameters progress:nil success:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,data);
    } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,nil);
    }];
}

+ (void)commitOrderWithInfo:(NSDictionary *)info
                   complete:(void (^)(ZCErrorCode status, NSString *message,id data))block {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithDictionary:info];
//    [parameters setValue:@"1.0" forKey:@"api_version"];
    [SKBaseWebService postRequest:@"/v1/order/submit" parameters:parameters progress:nil success:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,data);
    } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,nil);
    }];
}

+ (void)fetchOrderPayInfoWithOrderId:(NSString *)orderId
                            complete:(void (^)(ZCErrorCode status, NSString *message,id data))block {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    AppAndDeviceTool *tool = [AppAndDeviceTool shareInstance];
    
    [parameters setValue:orderId forKey:@"id"];
    [parameters setValue:tool.appVersionStr forKey:@"appVersion"];
    [parameters setValue:tool.appNameStr forKey:@"appName"];
    [parameters setValue:@"ios" forKey:@"systemType"];
    [parameters setValue:tool.deviceNameStr forKey:@"deviceName"];
    [parameters setValue:tool.connectionTypeStr forKey:@"connectionType"];
    [parameters setValue:tool.deviceIdStr forKey:@"deviceId"];
    [parameters setValue:tool.deviceModelStr forKey:@"deviceModel"];
    [parameters setValue:tool.deviceSystemVersionStr forKey:@"systemVersion"];
    [parameters setValue:@"iPhone" forKey:@"uiModel"];
    [parameters setValue:tool.channelStr forKey:@"channel"];
    [parameters setValue:tool.operatorStr forKey:@"operator"];
    [parameters setValue:tool.deviceTokenStr forKey:@"deviceToken"];
    
    [parameters setValue:@"1.0" forKey:@"api_version"];
    
    [SKBaseWebService getRequest:@"/v1/order/pay-order" parameters:parameters progress:nil success:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,data);
    } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,nil);
    }];
    
}

+ (void)postOrderPayInfoWithOrderSN:(NSString *)ordersn payType:(NSString *)payType paypwd:(NSString *)pwd useWallet:(NSString *)useWallet complete:(void (^)(ZCErrorCode, NSString *, id))block {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:ordersn forKey:@"order_sn"];
    [parameters setValue:payType forKey:@"pay_type"];
    [parameters setValue:pwd forKey:@"pwd"];
    [parameters setValue:useWallet forKey:@"use_wallet"];
    [SKBaseWebService postRequest:@"/v1/pay/pay" parameters:parameters progress:nil success:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,data);
    } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,nil);
    }];
}

+ (void)fetchOrderListWithStatus:(NSInteger)status
                        pageSize:(NSInteger)pageSize
                      lastItemid:(NSString *)ids
                           keywd:(NSString *)keywd
                        complete:(void (^)(ZCErrorCode status, NSString *message,id data))block {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:@(status) forKey:@"status"];
    [parameters setValue:@(pageSize) forKey:@"pageSize"];
    [parameters setValue:ids forKey:@"id"];
    [parameters setValue:keywd forKey:@"keywords"];
    [SKBaseWebService getRequest:@"/v1/order/list" parameters:parameters progress:nil success:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,data);
    } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,nil);
    }];
}

+ (void)canselOrderWithId:(NSString *)ids
                  causeId:(NSString *)causeId
                 complete:(void (^)(ZCErrorCode status, NSString *message,id data))block {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:ids forKey:@"id"];
    [parameters setValue:causeId forKey:@"cause"];
    [SKBaseWebService postRequest:@"/v1/order/cancel" parameters:parameters progress:nil success:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,data);
    } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,nil);
    }];
}


+ (void)delOrderWithId:(NSString *)ids
              complete:(void (^)(ZCErrorCode status, NSString *message,id data))block {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:ids forKey:@"id"];
    [SKBaseWebService postRequest:@"/v1/order/del" parameters:parameters progress:nil success:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,data);
    } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,nil);
    }];
}



+ (void)fetchOrderCanselResionWithComplete:(void (^)(ZCErrorCode status, NSString *message,id data))block {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:@(1) forKey:@"type"];
    [SKBaseWebService getRequest:@"/v1/order/reason" parameters:parameters progress:nil success:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,data);
    } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,nil);
    }];
}

+ (void)fetchOrderDetailWithOrderDict:(NSDictionary *)dict
                             complete:(void (^)(ZCErrorCode status, NSString *message,id data))block {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithDictionary:dict];
    [SKBaseWebService getRequest:@"/v1/order/detail" parameters:parameters progress:nil success:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,data);
    } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,nil);
    }];
}

+ (void)fetchLogisticsInfo:(NSString *)logisticsId
                 expressId:(NSString *)expressId
                  complete:(void (^)(ZCErrorCode status, NSString *message,id data))block {
    NSMutableDictionary *parameters = [self logicAuthParamWithSN:logisticsId];
    
    [SKBaseWebService thirdRequest:@"/api/getkuaidismart" urls:@"https://open.onebox.so.com" post:NO parameters:parameters progress:nil success:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        
    } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        
    }];
    
    

}



+ (void)confirmreceiptWithOrderId:(NSString *)orderId
                         complete:(void (^)(ZCErrorCode status, NSString *message,id data))block {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:orderId forKey:@"id"];

    [SKBaseWebService postRequest:@"/v1/order/confirm-receipt" parameters:parameters progress:nil success:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,data);
    } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,nil);
    }];
}

+ (void)autoCheckAddressWithString:(NSString *)address
                          complete:(void (^)(ZCErrorCode status, NSString *message,id data))block {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:address forKey:@"address"];
    [parameters setValue:@(0) forKey:@"referrer_uid"];
    
//    [SKBaseWebService getRequest:@"/v1/member-address/identify" parameters:parameters progress:nil success:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
//        block(status,msg,data);
//    } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
//        block(status,msg,nil);
//    }];
    [SKBaseWebService getRequest:@"/v1/area/address-parse" parameters:parameters progress:nil success:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,data);
    } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,nil);
    }];

}


+ (void)fetchGoodsCouponWithGoodsId:(NSString *)gid
                              cart_ids:(NSString *)cart_ids
                             goods_num:(NSString *)goods_num
                              complete:(void (^)(ZCErrorCode status, NSString *message,id data))block {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    
    if (cart_ids == nil || cart_ids.length == 0) {
        cart_ids = @"0";
    }
    [parameters setValue:gid forKey:@"goods_id"];
    [parameters setValue:goods_num forKey:@"goods_num"];
    [parameters setValue:cart_ids forKey:@"cart_ids"];
    [SKBaseWebService getRequest:@"/v1/order/goods-coupon" parameters:parameters progress:nil success:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,data);
    } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,nil);
    }];
}


///获取订单回执信息
+ (void)getOderReceiptWithOderNum:(NSString *)oderNum
                         complete:(void (^)(ZCErrorCode status, NSString *message,id data))block{
     NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        [parameters setValue:oderNum forKey:@"id"];

    [SKBaseWebService getRequest:@"/v1/order/order-receipt" parameters:parameters progress:nil success:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,data);
    } failure:^(ZCErrorCode status, NSString *msg, NSDictionary *data) {
        block(status,msg,nil);
    }];
}


+ (NSMutableDictionary *)logicAuthParamWithSN:(NSString *)sn {
    NSString *authKey = @"0a2ed9c731abcf0e82bf6237d637ab20";
    NSString *time = @(ceil([[NSDate date] timeIntervalSince1970])).stringValue;
    NSDictionary *dict = @{@"com":@"",
                           @"nu":sn,
                           @"time":time,
                           };
    NSString *pstr = @"";
    
    for (NSString *key in dict.allKeys) {
        pstr = [[pstr stringByAppendingString:key] stringByAppendingString:dict[key]];
    }
    pstr = [pstr stringByAppendingString:authKey];
    NSMutableDictionary *mdict = [dict mutableCopy];
    [mdict setValue:[self md5:pstr] forKey:@"token"];
    return mdict;
}

+ (NSString *)md5:(NSString *)str {
    const char *cStr = [str UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, strlen(cStr), result); // This is the md5 call
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}

@end
